<template>
  <div class="reports">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      <el-breadcrumb-item>数据报表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体 -->
    <el-card class="mt16">
      <div class="myCharts" ref="myChartDom"></div>
    </el-card>
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  name: "reports",
  data() {
    return {
      preChartData: {
        title: {
          text: "用户来源",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#E9EEF3",
            },
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            boundaryGap: false,
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
      },
    };
  },
  computed: {},
  methods: {
    async loadChart() {
      //1.获取存放图图表的容器
      const myChartDom = this.$refs.myChartDom;

      //2.将myChartDom初始化为echarts对象
      const chart = echarts.init(myChartDom);

      const res = await this.$http.getReportApi();
      const mergeData = { ...this.preChartData, ...res };
      console.log(mergeData, "图表数据");
      //3.配置echarts图表对象
      const options = mergeData;
      //4.渲染echarts图表  --setOptions
      chart.setOption(options);
      //5.图表随窗口尺寸改变而自适应
      window.addEventListener("resize", function () {
        // console.log("大小变了");
        chart.resize();
      });
    },
  },
  mounted() {
    this.loadChart();
  },
  created() {},
};
</script>
<style scoped lang="scss">
.myCharts {
  width: 100%;
  height: 400px;
  border: 1px solid #ccc;
}
</style>
